<template>
  <div class="resource-edit-container">
    <el-card class="edit-card">
      <h2 class="form-title">编辑资源</h2>
      <el-form :model="form" :rules="rules" ref="form" label-width="100px" class="resource-form">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入资源名称"></el-input>
        </el-form-item>
        <el-form-item label="地址" prop="url">
          <el-input v-model="form.url" placeholder="请输入资源地址"></el-input>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择类型" style="width: 100%;">
            <el-option label="菜单" value="1"></el-option>
            <el-option label="按钮" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number v-model="form.sort" :min="1" :max="999" placeholder="请输入排序号" style="width: 100%;"></el-input-number>
        </el-form-item>
        <el-form-item class="form-buttons">
          <el-button type="primary" @click="submitForm('form')" icon="el-icon-check">保存</el-button>
          <el-button @click="cancel" icon="el-icon-back">返回</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { getResourcesById, updateResources } from '@/api/resources'

export default {
  data() {
    return {
      form: {
        id: '',
        name: '',
        url: '',
        type: '',
        sort: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' },
          { min: 1, max: 25, message: '长度在 1 到 25 个字符', trigger: 'blur' }
        ],
        url: [
          { max: 50, message: '长度在 0 到 50 个字符', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '请选择类型', trigger: 'change' }
        ],
        sort: [
          { required: true, message: '请输入排序号', trigger: 'blur' },
          { type: 'number', min: 1, max: 999, message: '排序号必须在 1 到 999 之间', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.fetchResource()
  },
  methods: {
    async fetchResource() {
      try {
        const response = await getResourcesById(this.$route.params.id)
        const resourceData = response.data.data
        this.form = {
          id: resourceData.id,
          name: resourceData.name,
          url: resourceData.url,
          type: resourceData.type,
          sort: resourceData.sort,
          pid: resourceData.pid
        }
      } catch (error) {
        console.error('获取资源信息失败:', error)
        this.$message.error('获取资源信息失败')
      }
    },
    async submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          try {
            await updateResources(this.form)
            this.$message.success('更新资源成功')
            this.$router.push('/resources')
          } catch (error) {
            console.error('更新资源失败:', error)
            this.$message.error('更新资源失败')
          }
        }
      })
    },
    cancel() {
      this.$router.push('/resources')
    }
  }
}
</script>

<style scoped>
.resource-edit-container {
  padding: 20px;
}

.edit-card {
  margin-bottom: 20px;
}

.form-title {
  text-align: center;
  margin-bottom: 30px;
  color: #409EFF;
  font-size: 24px;
  font-weight: 600;
}

.resource-form {
  max-width: 600px;
  margin: 0 auto;
}

.el-form-item {
  margin-bottom: 20px;
}

.form-buttons {
  display: flex;
  justify-content: center;
  margin-top: 30px;
}

.form-buttons .el-button {
  margin: 0 10px;
  padding: 12px 24px;
  font-size: 14px;
}
</style>